package cn.edu.nuc.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.ssm.controller.page.PageControl;
import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.controller.page.Pc;
import cn.edu.nuc.ssm.dao.SysuserMapper;
import cn.edu.nuc.ssm.domain.Sysuser;
import cn.edu.nuc.ssm.service.interfaces.SysUserService;

@Service
public class SysUserServiceImpl implements SysUserService{
    @Autowired
	private SysuserMapper mapper;
    //分页模糊查询用户（通过用户名）
	@Override
	public Pager<Sysuser> userlist(String username, int current) {
		PageControl pc = new PageControl();
		Integer currindex = 1;
		if(current != 0){
			currindex = current;
		}
		pc.setCurrentindex(currindex);
		
		//查询总记录数。(如果带有名字，则查询改名字的总记录数。(模糊查询)
		int count = mapper.count("%"+username+"%");
		pc.setRscount(count);
		//初始化分页参数。
		pc = Pc.dealpage(pc);
		//查询具体内容
		List<Sysuser> user = mapper.selectSysuser("%"+username+"%",(pc.getCurrentindex()-1)*pc.getPagesize(),
				pc.getCurrentindex()*pc.getPagesize());
		System.out.println("service层集合大小" + user.size());
		Pager<Sysuser> pager = new Pager<Sysuser>();
		pager.setList(user);
		pager.setPagectrl(pc);
		return pager;
	}
	//后台添加用户
	@Override
	public int regist(Sysuser user) {
		return mapper.insert(user);
	}
	//通过id查询用户
	@Override
	public Sysuser selectByUserid(Integer userid) {
		return mapper.selectByPrimaryKey(userid);
	}
	//更新
	@Override
	public int updateByPrimaryKey(Sysuser user) {
		return mapper.updateByPrimaryKeySelective(user);
	}
	//通过主键删除用户
	@Override
	public int deleteByPrimaryKey(Integer userid) {
		System.out.println(userid);
		return mapper.deleteByPrimaryKey(userid);
	}
	
	
	//用户登录。。。。。
	@Override
	public List<Sysuser> login(String username, String password) {
		
		return mapper.login(username,password);
	}
	
	
	//Ajax   或得所有用户名。
	public List<String> getUsernames() {
		return mapper.getUsernames();
	}
    
    
}
